package com.drools.project.ruleService;

import com.drools.project.model.complex.Event;
import com.drools.project.model.complex.LegalStudent;
import com.drools.project.model.complex.Student;
import jakarta.annotation.Resource;
import org.drools.core.base.RuleNameEndsWithAgendaFilter;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.QueryResults;
import org.kie.api.runtime.rule.QueryResultsRow;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ComplexService {
    @Resource
    private KieContainer kieContainer;

    /**
     * query测试方法
     */
    public void complexQuery(List<Student> studentList) {
        KieSession kieSession = kieContainer.newKieSession();
        for (Student student : studentList) {
            kieSession.insert(student);
        }
        // 执行查询
        QueryResults results = kieSession.getQueryResults("findEligibleStudents");
        // 输出查询结果
        for (QueryResultsRow row : results) {
            System.out.println("有资格的学生: " + row.get("$name") + ", Age: " + row.get("$age") + ", Score: " + row.get("$score"));
        }
        kieSession.dispose();
    }

    /**
     * Function函数测试方法
     */
    public void complexFunction(List<Student> studentList) {
        // 创建全局变量
        LegalStudent legalStudent = new LegalStudent();
        legalStudent.setStudentList(new ArrayList<>());
        KieSession kieSession = kieContainer.newKieSession();
        kieSession.setGlobal("legalStudent", legalStudent);
        for (Student student : studentList) {
            kieSession.insert(student);
        }
        kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter("Check Student is eligible"));
        for (Student student : legalStudent.getStudentList()) {
            System.out.println("合规学生: " + student.getName() + ", Age: " + student.getAge() + ", Score: " + student.getScore());
        }
        kieSession.dispose();
    }

    /**
     * 规则优先级测试方法
     */
    public void complexPriority(List<Student> studentList) {
        KieSession kieSession = kieContainer.newKieSession();
        for (Student student : studentList) {
            kieSession.insert(student);
        }
        kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter("priorityRule"));
        kieSession.dispose();
    }

    /**
     * 规则组测试方法
     */
    public void complexRoleFlowGroup() {
        // 获取 KieSession 实例
        KieSession kSession = kieContainer.newKieSession();

        // 创建 Student 对象
        Student student1 = new Student("Alice", 20, 88, "Beijing");
        Student student2 = new Student("Bob", 22, 92, "Shanghai");
        Student student3 = new Student("Charlie", 21, 80, "Guangzhou");

        // 将 Student 对象插入到 KieSession 中
        kSession.insert(student1);
        kSession.insert(student2);
        kSession.insert(student3);
        // 设置焦点到 scholarshipCheck 规则组
        kSession.getAgenda().getAgendaGroup("scholarshipCheck").setFocus();
        // 触发规则
        kSession.fireAllRules(new RuleNameEndsWithAgendaFilter("ruleFlow-group"));
        // 关闭 KieSession
        kSession.dispose();
    }

    /**
     * 时间Event测试方法
     */
    public void complexEvent(List<Event> eventList) {
        KieSession kieSession = kieContainer.newKieSession();
        for (Event event : eventList) {
            kieSession.insert(event);
        }
        // 触发规则
        kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter("Process Login Event"));
        // 关闭会话
        kieSession.dispose();
    }

}
